package com.atwei.mango.system.exception;

import com.atwei.mango.system.apiModel.response.ApiResponse;
import lombok.extern.log4j.Log4j2;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Classname TopException
 * @Description 控制层全局异常处理
 * @Date 2022/6/7 19:05
 * @Author liuwei
 */
@ControllerAdvice
@Log4j2
public class TopException<T> {

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ApiResponse<T> handle(Exception e){
        if (e instanceof ApiException){
            log.error("业务日志:",e);
            return ApiResponse.error(ResponseEnum.ERROR);
        }else if (e instanceof AccessDeniedException){
            log.error("权限不足:",e);
            return ApiResponse.error(ResponseEnum.DENIED);
        }
        log.error("系统日志:",e);
        return ApiResponse.error(ResponseEnum.BUSINESS);
    }
}
